<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use October\Rain\Support\Facades\Input;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hjd\Models\TfRuleModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Config;
class TfRuleController extends Controller
{
    use ApiReturn;

    /**
     * 转账规则
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $query=TfRuleModel::select();
        $query->with([
            'from_currency'=>function($q){
                $q->select('slug','title');
            },
            'to_currency'=>function($q){
                $q->select('slug','title');
            },
        ]);
        $query->whereHas('from_currency',function ($q){
            $q->where('status',1);
        });
        $query->whereHas('to_currency',function ($q){
            $q->where('status',1);
        });
        //可传参数，
        if(Input::has('tf_type')){
            $tf_type=Input::get('tf_type');
            $query->where('tf_type',$tf_type);
        }
        $query->where('status',1);
        $records=$query->get();
        $records->each(function ($record){
            $record->tf_type_text=Config::get('summer.hjd::tf_type.'.$record->tf_type);
        });
        $return['records']=$records;
        return $this->apiSuccess('操作成功',$return);
    }
}
